<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 正则路由类
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Controller_Router_Route_Regex extends EGP_Controller_Router_Route_Abstract
{

    /**
     * 正则匹配参数获取规则
     *
     * @var array
     */
    protected $_reqs = array();

    /**
     * 设置正则匹配的参数获取规则
     *
     * @param  array  $reqs
     * @return EGP_Controller_Router_Route_Regex
     */
    public function setReqs(array $reqs)
    {
        foreach ($reqs as $key => $value) {
            $this->_reqs[$key] = $value;
        }

        return $this;
    }

    /**
     * 测试是否匹配路径
     *
     * @param  string  $host
     * @param  string  $path
     * @return boolean
     */
    public function match($host, $path)
    {
        if ($this->inHosts($host) &&
            preg_match('#^' . $this->_route . '$#iu', ltrim($path, '/'), $maches)) {
            foreach ($maches as $key => $value) {
                if (isset($this->_reqs[$key]) && !empty($value)) {
                    $this->setDefault($this->_reqs[$key], $value);
                }
            }
            return true;
        }
        return false;
    }

}